package DCSource.Template;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import DCSource.dcsrc;
import DCSource.Template.Input.Transmitter;
import DCSource.Template.SRC.Converter;

public class CMD extends JFrame{
	private Converter Text = new Converter(5,5,90,70);
	private Converter Input = new Converter(5,80,90,10);
	
	private JTextArea box = new JTextArea();
	private JTextField field = new JTextField();
	private int maxLines=0;
	private ArrayList<Transmitter> values = new ArrayList<Transmitter>();
	public CMD(String title,int width,int height,boolean CloseOnExit,int maxLines){
		this.maxLines = maxLines;
		this.setTitle(title);
		this.setSize(width,height);
		if (CloseOnExit){
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		this.setLayout(null);
		this.setResizable(false);
		box.setEditable(false);
		JScrollPane pane = new JScrollPane(box,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		Text.ConvertIn(this.getHeight(), this.getWidth(), pane);
		Input.ConvertIn(this.getHeight(), this.getWidth(), field);
		this.add(pane);
		this.add(field);
		final CMD getIn = this;
		field.addKeyListener(new KeyListener(){
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyChar()==10 | arg0.getKeyChar()==13){
					String getting = field.getText();
					for (int I=0;I<values.size();I++){
						values.get(I).get(getting,getIn);
					}
					field.setText("");
				}
			}
			public void keyReleased(KeyEvent arg0) {
				
			}
			public void keyTyped(KeyEvent arg0) {
				
			}});
		this.show();
	}
	public void setMaxLines(int maxLines){
		this.maxLines = maxLines;
	}
	public void writeLn(String str){
		String text = box.getText();
		text += str + DCSource.dcsrc.getUmschalt();
		if (text.split(DCSource.dcsrc.getUmschalt()).length>maxLines){
			String[] ln = text.split(DCSource.dcsrc.getUmschalt());
			text="";
			for (int i=1;i<=maxLines;i++){
				text+=ln[i]+dcsrc.getUmschalt();
			}
		}
		box.setText(text);
	}
	public void write(String str){
		String text = box.getText();
		text += str;
		box.setText(text);
	}
	public void clear(){
		box.setText("");
	}
	public void add(Transmitter b){
		values.add(b);
	}
	public void rev(Transmitter b){
		values.remove(b);
	}
	public String getAsHTML(){
		String Page = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
		Page += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
		Page += "<head>";
		Page += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		Page += "<title>Exportet CMD>> "+this.getTitle()+"</title>";
		Page += "</head>";
		Page += "<body>";
		Page += "<div style=\"position: absolute; height: 50px; width: "+this.getWidth()+"px; left: 50%; margin-left: -"+(int)(Math.round(this.getWidth()/2))+"px; border-color: #999; border: medium; background-color: #CCC; overflow:auto;\">"+this.getTitle()+"</div>";
		Page += "<div style=\"position: absolute; height: "+this.getHeight()+"px; width: "+this.getWidth()+"px; border-color: #0FF; border: groove; top: 70px; left: 50%; margin-left: -"+(int)(Math.round(this.getWidth()/2))+"px; background: #CCC;\">";
		Page += "<div style=\"position: absolute; top: 5%; left: 5%; width: 90%; height: 70%; background: #FFF; overflow:auto;\">";
		String text = box.getText();
		String[] ln = text.split(DCSource.dcsrc.getUmschalt());
		for (int i=0;i<=ln.length-1;i++){
			Page+=ln[i]+"<br />";
		}
		Page += "</div>";
		Page += "<div style=\"position: absolute; top: 80%; left: 5%; width: 90%; height: 10%; background: #FFF; overflow:auto;\">";
		Page += field.getText();
		Page += "</div></div>";
		Page += "</body>";
		Page += "</html>";
		return Page;
	}
	public DCSource.ConfigManager.Class getAsManagerClass(){
		DCSource.ConfigManager.Class Source = new DCSource.ConfigManager.Class(this.getTitle());
		String text = box.getText();
		String[] ln = text.split(DCSource.dcsrc.getUmschalt());
		for (int i=0;i<=ln.length-1;i++){
			Source.addItem(new DCSource.ConfigManager.Item("ln"+(i+1),ln[i]), true);
		}
		Source.addItem(new DCSource.ConfigManager.Item("Inputfield",field.getText()), true);
		return Source;
	}
}
